package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.AttendClassRecordDetail;
import com.yuyou.fn.educational.mapper.AttendClassRecordDetailMapper;
import com.yuyou.fn.educational.service.IAttendClassRecordDetailService;
import com.yuyou.fn.educational.vo.AttendClassRecordDetailQueryVo;
import com.yuyou.fn.educational.vo.AttendClassRecordVo;
import com.yuyou.fn.platform.permission.DataPermission;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * AttendClassRecordDetail 表数据服务层接口实现类
 *
 */
@Service
public class AttendClassRecordDetailServiceImpl extends SuperServiceImpl<AttendClassRecordDetailMapper, AttendClassRecordDetail,String> implements IAttendClassRecordDetailService {


    @Override
    public Page<AttendClassRecordDetail> findAttendClassRecordDetailForPage(AttendClassRecordDetailQueryVo attendClassRecordDetailQueryVo, DataPermission dataPermission, int pageNo, int pageSize) {
        Example<AttendClassRecordDetail> example = AttendClassRecordDetail.createExample();
        example.orderBy("classId");
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillConditionForBusPermission(criteria, dataPermission.getBusTeamIds());
        CriteriaUtils.fillCondition(criteria, attendClassRecordDetailQueryVo);

        return findPageByExample(example,pageNo,pageSize);
    }

    @Override
    public SXSSFWorkbook exportAttendanceRecordDetail(List<AttendClassRecordVo> attendClassRecordDetailList) {
        SXSSFWorkbook excel = new SXSSFWorkbook();
        SXSSFSheet sheet = excel.createSheet("报名记录");

        String[] titles = new String[] { "学生","学生手机号码","班级状态","考勤","备注" };

        Map<Integer,String> attendanceStatusMap = new HashMap<>();
        attendanceStatusMap.put(0,"出勤");
        attendanceStatusMap.put(1,"缺勤");
        attendanceStatusMap.put(2,"迟到");
        attendanceStatusMap.put(3,"请假");

        Map<Integer,String> shiftStatusMap = new HashMap<>();
        shiftStatusMap.put(0,"正常");
        shiftStatusMap.put(1,"调出");
        shiftStatusMap.put(2,"调入");

        int rowIndex  =  0 ;
        int columnIndex = 0 ;

        SXSSFRow row = sheet.createRow(rowIndex ++ );
        for(String head : titles){
            SXSSFCell cell = row.createCell(columnIndex++);
            cell.setCellValue(head);
        }

        if (attendClassRecordDetailList!=null){
            for(AttendClassRecordVo attendClassRecordVo : attendClassRecordDetailList){
                columnIndex = 0 ;
                row = sheet.createRow(rowIndex ++ );

                SXSSFCell cell = row.createCell(columnIndex++);
                cell.setCellValue(attendClassRecordVo.getStudentName());

                cell = row.createCell(columnIndex++);
                cell.setCellValue(attendClassRecordVo.getStudentPhoneNo());

                cell = row.createCell(columnIndex++);
                cell.setCellValue(attendClassRecordVo.getShiftStatus()==null?"正常":shiftStatusMap.get(attendClassRecordVo.getShiftStatus()));

                cell = row.createCell(columnIndex++);
                if(attendClassRecordVo.getIsAttendance() != null && attendClassRecordVo.getIsAttendance() == 1 ){
                    cell.setCellValue(attendanceStatusMap.get(attendClassRecordVo.getAttendanceStatus()));
                }else{
                    cell.setCellValue(StringUtils.EMPTY);
                }

                cell = row.createCell(columnIndex++);
                cell.setCellValue(StringUtils.defaultString(attendClassRecordVo.getRemark()));

            }
        }

        return excel;
    }

    @Override
    public List<AttendClassRecordDetail> findAttendClassRecordDetailByQueryVo(List<Integer> shiftStatusList, AttendClassRecordDetailQueryVo attendClassRecordDetailQueryVo) {
        if (Utils.checkListEmpty(shiftStatusList)){
            return Collections.EMPTY_LIST;
        }
        Example<AttendClassRecordDetail> example = AttendClassRecordDetail.createExample();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria, attendClassRecordDetailQueryVo);
        criteria.andIn("shiftStatus",shiftStatusList);
        example.orderBy("classDate").orderBy("startTime");
        return findListByExample(example);
    }







}